import 'package:situation/shared/shared.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'state.dart';

class UserPrivacyPermissionController extends GetxController {
  final UserPrivacyPermissionState state = UserPrivacyPermissionState();

  @override
  Future<void> onInit() async {
    // TODO: implement onInit
    super.onInit();

    PermissionStatus caremaPermissionStatus = await Permission.camera.status;
    state.caremaStatus = caremaPermissionStatus.isGranted;

    PermissionStatus storagePermissionStatus = await Permission.storage.status;
    state.storageStatus = storagePermissionStatus.isGranted;

    PermissionStatus audioPermissionStatus = await Permission.microphone.status;
    state.audioStatus = audioPermissionStatus.isGranted;

    PermissionStatus locationPermissionStatus =
        await Permission.location.status;
    state.locationStatus = locationPermissionStatus.isGranted;

    state.readStatus =
        UtilSp.getBool(AppConstant.TracelessReading, defValue: false)!;
    state.searchStatus =
        UtilSp.getBool(AppConstant.TracelessSearch, defValue: false)!;
  }

  void requestPermission(Permission permission) async {
    switch (permission) {
      case Permission.camera:
        if (state.caremaStatus) {
          // 跳转设置
          showGeneralDialog(
            context: Get.context!,
            pageBuilder: (BuildContext buildContext,
                Animation<double> animation,
                Animation<double> secondaryAnimation) {
              return TDAlertDialog(
                content: '关闭后，将无法录制视频、拍摄照片及无法使用与该权限相关的功能',
                rightBtnAction: () {
                  openAppSettings();
                  Get.back();
                },
              );
            },
          );
        } else {
          PermissionStatus caremaPermissionStatus = await permission.request();
          state.caremaStatus = caremaPermissionStatus.isGranted;
        }
        break;
      case Permission.storage:
        if (state.storageStatus) {
          // 跳转设置
          // 跳转设置
          showGeneralDialog(
            context: Get.context!,
            pageBuilder: (BuildContext buildContext,
                Animation<double> animation,
                Animation<double> secondaryAnimation) {
              return TDAlertDialog(
                content: '关闭后，可能影响发布/保存/修改图文、文件等功能',
                rightBtnAction: () {
                  openAppSettings();
                  Get.back();
                },
              );
            },
          );
        } else {
          PermissionStatus storagePermissionStatus = await permission.request();
          state.storageStatus = storagePermissionStatus.isGranted;
        }
        break;
      case Permission.microphone:
        if (state.audioStatus) {
          // 跳转设置
          // 跳转设置
          showGeneralDialog(
            context: Get.context!,
            pageBuilder: (BuildContext buildContext,
                Animation<double> animation,
                Animation<double> secondaryAnimation) {
              return TDAlertDialog(
                content: '关闭后，将无法发布音视频信息',
                rightBtnAction: () {
                  openAppSettings();
                  Get.back();
                },
              );
            },
          );
        } else {
          PermissionStatus storagePermissionStatus = await permission.request();
          state.audioStatus = storagePermissionStatus.isGranted;
        }
        break;
      case Permission.location:
        if (state.locationStatus) {
          // 跳转设置
          // 跳转设置
          showGeneralDialog(
            context: Get.context!,
            pageBuilder: (BuildContext buildContext,
                Animation<double> animation,
                Animation<double> secondaryAnimation) {
              return TDAlertDialog(
                content: '关闭后，将无法为你提供基于精确位置的地理位置功能或服务',
                rightBtnAction: () {
                  openAppSettings();
                  Get.back();
                },
              );
            },
          );
        } else {
          PermissionStatus storagePermissionStatus = await permission.request();
          state.locationStatus = storagePermissionStatus.isGranted;
        }
        break;
    }
  }

  bool updateReadStatus(bool value) {
    state.readStatus = value;
    UtilSp.putBool(AppConstant.TracelessReading, value);
    return value;
  }

  bool updateSearchStatus(bool value) {
    state.searchStatus = value;
    UtilSp.putBool(AppConstant.TracelessSearch, value);
    return value;
  }
}
